﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace tjb.Utility
{
    /// <summary>
    /// 基本环境参数以及操作方法
    /// </summary>
    public sealed class Util
    {
        private Util() { }
        /// <summary>
        ///  将 URL 转换为在请求客户端可用的 URL。
        ///  <para>
        ///  同Control.ResolveUrl(string);
        ///  </para>
        /// </summary>
        /// <param name="relativeUrl"></param>
        /// <returns> 转换后的 URL。</returns>
        /// <exception cref="ArgumentNullException"> 当 relativeUrl 参数包含 null 时发生。</exception>
        public static string ResolveUrl(string relativeUrl)
        {
            if (ApplicationPath != string.Empty)
                return ApplicationPath.TrimEnd('/', '\\') + "/" + relativeUrl.TrimStart('/', '\\', '~');
            else
                return relativeUrl;
        }


        private static string VitulPath = null;
        /// <summary>
        /// 获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。
        /// </summary>
        public static string ApplicationPath
        {
            get
            {
                if (VitulPath == null)
                {
                    if (HttpContext.Current != null && HttpContext.Current.Request != null)
                        VitulPath = HttpContext.Current.Request.ApplicationPath;
                    else
                        return string.Empty;
                }
                return VitulPath;
            }
        }
    }
}
